package com.swt.testdemo.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swt.testdemo.config.ResponseBean;
import com.swt.testdemo.config.ResponseMessage;
import com.swt.testdemo.constant.FileConstant;
import com.swt.testdemo.dao.SysOntologyAttributeDao;
import com.swt.testdemo.dao.SysOntologyDao;
import com.swt.testdemo.dao.SysOntologyIconDao;
import com.swt.testdemo.entity.SysDataTemplateEntity;
import com.swt.testdemo.entity.SysOntologyAttributeEntity;
import com.swt.testdemo.entity.SysOntologyEntity;
import com.swt.testdemo.entity.SysOntologyIconEntity;
import com.swt.testdemo.form.EditEntityForm;
import com.swt.testdemo.service.SysDataTemplateService;
import com.swt.testdemo.service.SysOntologyIconService;
import com.swt.testdemo.service.SysOntologyService;
import com.swt.testdemo.util.RedisUtil;
import com.swt.testdemo.vo.SysOntologyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author BleethNie
 * @version 1.0
 * @date 2020/11/13 10:26
 **/
@Service("")
public class SysOntologyServiceImpl extends ServiceImpl<SysOntologyDao, SysOntologyEntity> implements SysOntologyService {


    @Autowired
    private SysOntologyDao sysOntologyDao;


    @Autowired
    private SysOntologyAttributeDao sysOntologyAttributeDao;


    @Autowired
    private SysOntologyIconDao sysOntologyIconDao;


    @Autowired
    private SysDataTemplateService sysDataTemplateService;


    @Autowired
    private SysOntologyIconService sysOntologyIconService;


    @Autowired
    private RedisUtil redisUtil;


    @Override
    public ResponseBean getEntityList() {
        List<SysOntologyEntity> sysOntologyEntityList = this.list();
        ResponseBean response = new ResponseBean(200,"查询成功",sysOntologyEntityList);
        return response;
    }

    @Override
    public ResponseBean getEntityInfo(Integer entityId) {
        String key = "ontology:entityId:"+entityId;
        SysOntologyVo sysOntologyVo = redisUtil.getObject(key, SysOntologyVo.class);
        if (sysOntologyVo == null){
            SysOntologyEntity sysOntologyEntity = this.getById(entityId);

           //属性查询
            QueryWrapper<SysOntologyAttributeEntity> queryWrapper  = new QueryWrapper<>();
            queryWrapper.eq("ontology_id", entityId);
            queryWrapper.orderByAsc("sort_num");
            queryWrapper.orderByDesc("show_status");
            queryWrapper.orderByDesc("update_time");
            List<SysOntologyAttributeEntity> sysOntologyAttributeEntities = sysOntologyAttributeDao.selectList(queryWrapper);

            sysOntologyVo = new SysOntologyVo();
            BeanUtils.copyProperties(sysOntologyEntity,sysOntologyVo);
            sysOntologyVo.setAttributeEntityList(sysOntologyAttributeEntities);


            //图标获取
            SysOntologyIconEntity sysOntologyIconEntity = sysOntologyIconDao.selectOntologyIcon(entityId);
            if (sysOntologyIconEntity !=null){
               sysOntologyVo.setIconId(sysOntologyIconEntity.getId());
               sysOntologyVo.setIconScale(sysOntologyIconEntity.getIconScale());
               sysOntologyVo.setOntologyIcon(sysOntologyIconEntity.getOntologyIcon());

            }
            redisUtil.setObject(key,sysOntologyVo,RedisUtil.DEFAULT_EXPIRE_TIME);


        }

        return new ResponseBean(200,"查询成功",sysOntologyVo);
    }

    @Override
    public ResponseBean createEntity(String ontologyName) {

        //创建实体
        SysOntologyEntity sysOntologyEntity = new SysOntologyEntity();
        sysOntologyEntity.setOntologyName(ontologyName);
        DateTime current = DateUtil.date();
        sysOntologyEntity.setCreatedTime(current);
        sysOntologyEntity.setCreatedTime(current);
        sysOntologyEntity.setUpdateTime(current);
        this.save(sysOntologyEntity);

        //创建sys_data_template中未分类模板
        SysDataTemplateEntity sysDataTemplateEntity = new SysDataTemplateEntity();
        sysDataTemplateEntity.setOntologyId(sysOntologyEntity.getId());
        sysDataTemplateEntity.setTemplateName("未分类");
        sysDataTemplateEntity.setTemplateType(SysDataTemplateEntity.TEMPLATE_TYPE_UNSORT);
        sysDataTemplateEntity.setCreatedTime(current);
        sysDataTemplateEntity.setUpdateTime(current);
        sysDataTemplateService.save(sysDataTemplateEntity);


        return new ResponseBean(200,"创建成功",sysOntologyEntity);
    }

    @Override
    public ResponseBean uploadEntityPic(Integer entityId, MultipartFile file) {
        InputStream inputStream = null;
        try {
            inputStream= file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        long time = DateUtil.current(false);
        String headName = file.getOriginalFilename();
        String currentName = time + "_" + headName;

        String userPath = StrUtil.format(FileConstant.ONTOLOGY_RESOURCE_PATH + "{}/{}", entityId, currentName);
        String parent = FileUtil.getParent(userPath, 1);
        if (!FileUtil.exist(parent)){
            File mkdir = FileUtil.mkdir(new File(parent));
        }

        String urlPath = StrUtil.format("ontology/{}/{}" + entityId, currentName);
        FileUtil.writeFromStream(inputStream,userPath);

        //写入数据库
        DateTime current = DateUtil.date();
        SysOntologyIconEntity sysOntologyIconEntity = new SysOntologyIconEntity();
        sysOntologyIconEntity.setCreatedTime(current);
        sysOntologyIconEntity.setUpdateTime(current);
        sysOntologyIconEntity.setOntologyIcon(userPath);
        sysOntologyIconEntity.setIconScale(10);
        sysOntologyIconEntity.setStatus("0");
        sysOntologyIconEntity.setId(entityId);
        sysOntologyIconService.save(sysOntologyIconEntity);


        return new ResponseBean(200,"上传成功",sysOntologyIconEntity);
    }

    @Override
    public ResponseMessage deleteEntity(Integer entityId) {
        String key ="ontology:entityId:"+entityId;
        redisUtil.del(key);

        //删除属性
        QueryWrapper<SysOntologyAttributeEntity> attributeQw = new QueryWrapper<>();
        attributeQw.eq("ontology_id",entityId);
        sysOntologyAttributeDao.delete(attributeQw);

        //删除本身
        boolean success = this.removeById(entityId);


        //删除图标记录
        QueryWrapper<SysOntologyIconEntity> iconQw = new QueryWrapper<>();
        iconQw.eq("ontology_id",entityId);
        sysOntologyIconDao.delete(iconQw);

        //删除相关的图标库
        String ontologyIconPath = StrUtil.format(FileConstant.ONTOLOGY_RESOURCE_PATH + "/{}", entityId);
        FileUtil.del(ontologyIconPath);



        return new ResponseMessage(200,"删除成功");
    }

    @Override
    public ResponseMessage editEntity(EditEntityForm form) {
        String key = "ontology:entityId:"+form.getEntityId();
        redisUtil.del(key);

        DateTime current = DateUtil.date();
        Integer entityId = form.getEntityId();

        SysOntologyEntity sysOntologyEntity =new SysOntologyEntity();
        BeanUtils.copyProperties(form,sysOntologyEntity);
        sysOntologyEntity.setId(entityId);
        sysOntologyEntity.setUpdateTime(current);
        this.updateById(sysOntologyEntity);

        Integer iconId = form.getIconId();
        Integer iconScale = form.getIconScale();

        SysOntologyIconEntity sysOntologyIconEntity = new SysOntologyIconEntity();
        sysOntologyIconEntity.setId(iconId);
        sysOntologyIconEntity.setIconScale(iconScale);
        sysOntologyIconEntity.setUpdateTime(current);
        sysOntologyIconService.updateById(sysOntologyIconEntity);

        return new ResponseMessage(200,"编辑成功");
    }
}
